local skel = fk.CreateSkill {
  name = "emo__tingying",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__tingying"] = "听荧",
  [":emo__tingying"] = "弃牌阶段开始时，你可以将一张牌当目标上限+1【火攻】或【无中生有】使用；若使此阶段从无需/需要弃牌变至另一者，其他角色下个弃牌阶段你须再次发动“听荧”，若仍变至另一者，你失去1点体力。",

  ["@@emo__tingying"] = "听荧",
  ["#emo__tingying-ask"] = "听荧：将1张牌当目标上限+1的【火攻】或【无中生有】使用",
  ["emo__tingying_active"] = "听荧",
}

--- 判断某角色是否需要弃牌
---@param player Player
---@return boolean
local willDiscard = function (player)
  return #table.filter(
    player:getCardIds(Player.Hand), function(id)
      local card = Fk:getCardById(id)
      return table.every(Fk:currentRoom().status_skills[MaxCardsSkill] or Util.DummyTable, function(skill)
        return not skill:excludeFrom(player, card)
      end)
    end
  ) > player:getMaxCards()
end

--- 获取转化技默认值
---@param player Player
local getDefaultData = function (player)
  local cards = player:getCardIds("he")
  if #cards == 0 then return nil end
  for _, id in ipairs(cards) do
    local c = Fk:cloneCard("ex_nihilo")
    c.skillName = skel.name
    c:addSubcard(id)
    if player:canUseTo(c, player) then
      return {
        cards = {id}, from = player, targets = {player}, interaction = "ex_nihilo"
      }
    end
  end
  local players = table.simpleClone(Fk:currentRoom().alive_players)
  if table.removeOne(players, player) then
    table.insert(players, player)
  end
  for _, p in ipairs(players) do
    if not p:isKongcheng() then
      for _, id in ipairs(cards) do
        local c = Fk:cloneCard("fire_attack")
        c.skillName = skel.name
        c:addSubcard(id)
        if player:canUseTo(c, p) then
          return {
            cards = {id}, from = player, targets = {p}, interaction = "fire_attack"
          }
        end
      end
    end
  end
end


skel:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if player:isNude() then
      return false
    end
    if target.phase == Player.Discard and player:hasSkill(skel.name) then
      return target == player or player:getMark("@@emo__tingying") ~= 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cancelable = true
    if target ~= player then -- 其他角色的弃牌阶段发动的情况
      room:setPlayerMark(player, "@@emo__tingying", 0)
      cancelable = false
    end
    local _ , dat = room:askToUseActiveSkill(player, {
      skill_name = "emo__tingying_active", prompt = "#emo__tingying-ask", cancelable = cancelable,
    })
    if not dat and not cancelable then
      dat = getDefaultData(player) -- 获取转化技默认值
    end
    if not dat then return false end
    event:setCostData(self, {tos = dat.targets, name = dat.interaction, cards = dat.cards })
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local will1 = willDiscard(target)
    local dat = event:getCostData(self)
    if not dat then return false end
    local card = Fk:cloneCard(dat.name)
    card.skillName = skel.name
    card:addSubcard(dat.cards[1])
    room:useCard{
      from = player, tos = dat.tos, card = card
    }
    if player.dead then return end
    if willDiscard(target) ~= will1 then
      if target == player then
        room:setPlayerMark(player, "@@emo__tingying", 1)
      else
        room:loseHp(player, 1, skel.name)
      end
    end
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@@emo__tingying", 0)
end)

local skel2 = fk.CreateSkill {
  name = "emo__tingying_active",
}

skel2:addEffect("active", {
  interaction = function(self, player)
    return UI.ComboBox { choices = {"fire_attack", "ex_nihilo"} }
  end,
  card_filter = function (self, player, to_select, selected)
    local choice = self.interaction.data
    if choice == nil or #selected > 0 then return false end
    local card = Fk:cloneCard(choice)
    card.skillName = skel.name
    card:addSubcard(to_select)
    return not player:prohibitUse(card)
  end,
  target_filter = function (self, player, to_select, selected)
    local choice = self.interaction.data
    if choice == nil then return false end
    local card = Fk:cloneCard(choice)
    card.skillName = skel.name
    if choice == "ex_nihilo" and not table.contains(selected, player) and to_select ~= player then
      return false -- 无中必须选自己
    end
    return #selected < 2 and player:canUseTo(card, to_select)
  end,
  card_num = 1,
  min_target_num = 1,
})

return {skel, skel2}
